
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
<div class="archive_info">
<h1>JavaScript <span class="color_h1">函数</span>
</h1>
<hr/>
<p class="intro">函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块。</p>
<div class="example"><h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-code">!</span><span class="hl-var">DOCTYPE</span><span class="hl-code"> </span><span class="hl-var">html</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">html</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">head</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">meta</span><span class="hl-code"> </span><span class="hl-var">charset</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">utf-8</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-code">测试实例</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">script</span><span class="hl-brackets">&gt;</span><span class="hl-code">
function myFunction()
{
    alert("Hello World!");
}
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">script</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">head</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">body</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">button</span><span class="hl-code"> </span><span class="hl-var">onclick</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">myFunction()</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">点我</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">button</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">body</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">html</span><span class="hl-brackets">&gt;</span></div>
</div><br/><a class="tryitbtn"  rel="noopener noreferrer" target="_blank">尝试一下 »</a>
</div>
<br/><hr/>
<h2>JavaScript 函数语法</h2>
<p>函数就是包裹在花括号中的代码块，前面使用了关键词 function：</p>
<div class="code"><div>

function <i>functionname</i>()<br/>
{<br/>    <i>// 执行代码</i><br/>
}</div></div>
<p>当调用该函数时，会执行函数内的代码。</p>
<p>可以在某事件发生时直接调用函数（比如当用户点击按钮时），并且可由 JavaScript 在任何位置进行调用。</p>
<table class="lamp"><tr>
<th width="34"><img alt="lamp" decoding="async" height="32" src="/images/lamp.jpg" width="32"/></th>
<td>JavaScript 对大小写敏感。关键词 function 必须是小写的，并且必须以与函数名称相同的大小写来调用函数。</td>
</tr></table>
<br/><hr/>
<h2>调用带参数的函数</h2>
<p>在调用函数时，您可以向其传递值，这些值被称为参数。</p>
<p>这些参数可以在函数中使用。</p>
<p>您可以发送任意多的参数，由逗号 (,) 分隔：</p>
<div class="code"><div>
	myFunction(<i>argument1,argument2</i>)<br/>
</div></div>
<p>当您声明函数时，请把参数作为变量来声明： </p>
<div class="code"><div>
function myFunction(<em><span class="marked">var1</span></em>,<em><span class="marked">var2</span></em>)<br/>{<br/><i>代码</i><br/>
}</div></div>
<p>变量和参数必须以一致的顺序出现。第一个变量就是第一个被传递的参数的给定的值，以此类推。</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">p</span><span class="hl-brackets">&gt;</span><span class="hl-code">点击这个按钮，来调用带参数的函数。</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">p</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">button</span><span class="hl-code"> </span><span class="hl-var">onclick</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">myFunction('Harry Potter','Wizard')</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">点击这里</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">button</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">script</span><span class="hl-brackets">&gt;</span><span class="hl-code">
function myFunction(name,job){
    alert("Welcome " + name + ", the " + job);
}
</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">script</span><span class="hl-brackets">&gt;</span></div></div>
<br/><a class="tryitbtn"  rel="noopener noreferrer" target="_blank">尝试一下 »</a>
</div>
<p>上面的函数在按钮被点击时会提示 "Welcome Harry Potter, the Wizard"。</p>
<p>函数很灵活，您可以使用不同的参数来调用该函数，这样就会给出不同的消息：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-brackets">&lt;</span><span class="hl-reserved">button</span><span class="hl-code"> </span><span class="hl-var">onclick</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">myFunction('Harry Potter','Wizard')</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">点击这里</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">button</span><span class="hl-brackets">&gt;</span><span class="hl-code">
</span><span class="hl-brackets">&lt;</span><span class="hl-reserved">button</span><span class="hl-code"> </span><span class="hl-var">onclick</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-string">myFunction('Bob','Builder')</span><span class="hl-quotes">"</span><span class="hl-brackets">&gt;</span><span class="hl-code">点击这里</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">button</span><span class="hl-brackets">&gt;</span></div></div>
<br/><a class="tryitbtn"  rel="noopener noreferrer" target="_blank">尝试一下 »</a>
</div>
<p>根据您点击的不同的按钮，上面的例子会提示 "Welcome Harry Potter, the Wizard" 或 "Welcome Bob, the Builder"。</p>
<hr/>
<h2>带有返回值的函数</h2>
<p>有时，我们会希望函数将值返回调用它的地方。</p>
<p>通过使用 return 语句就可以实现。</p>
<p>在使用 return 语句时，函数会停止执行，并返回指定的值。</p>
<h3>语法</h3>
<div class="code"><div>

function myFunction()<br/>
{<br/>
    var x=5;<br/>    return x;<br/>}</div></div>
<p>上面的函数会返回值 5。</p>
<p><strong>注意：</strong> 整个 JavaScript 并不会停止执行，仅仅是函数。JavaScript 将继续执行代码，从调用函数的地方。</p>
<p>函数调用将被返回值取代：</p>
<div class="code"><div>

	var myVar=myFunction();</div></div>
<p>myVar 变量的值是 5，也就是函数 "myFunction()" 所返回的值。</p>
<p>即使不把它保存为变量，您也可以使用返回值：</p>
<div class="code"><div>

	document.getElementById("demo").innerHTML=myFunction();</div></div>
<p>"demo" 元素的 innerHTML 将成为 5，也就是函数 "myFunction()" 所返回的值。</p>
<p>您可以使返回值基于传递到函数中的参数：</p>
<div class="example">
<h2 class="example">实例</h2>
<p>计算两个数字的乘积，并返回结果：</p>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">myFunction</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">*</span><span class="hl-identifier">b</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-builtin">document</span><span class="hl-code">.</span><span class="hl-identifier">getElementById</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">demo</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">.</span><span class="hl-identifier">innerHTML</span><span class="hl-code">=</span><span class="hl-identifier">myFunction</span><span class="hl-brackets">(</span><span class="hl-number">4</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div>
</div>
<p>"demo" 元素的 innerHTML 将是：</p>
<div class="example_code">
12
</div>
<br/><a class="tryitbtn"  rel="noopener noreferrer" target="_blank">尝试一下 »</a>
</div>
<p>在您仅仅希望退出函数时 ，也可使用 return 语句。返回值是可选的：</p>
<div class="example">
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">myFunction</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">,</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">&gt;</span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">return</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-identifier">a</span><span class="hl-code">+</span><span class="hl-identifier">b</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
</div>
</div>
<p>如果 a 大于 b，则上面的代码将退出函数，并不会计算 a 和 b 的总和。</p>
<hr/>
<h2>局部 JavaScript 变量</h2>
<p>在 JavaScript 函数内部声明的变量（使用 var）是<em>局部</em>变量，所以只能在函数内部访问它。（该变量的作用域是局部的）。</p>
<p>您可以在不同的函数中使用名称相同的局部变量，因为只有声明过该变量的函数才能识别出该变量。</p>
<p>只要函数运行完毕，本地变量就会被删除。</p>
<hr/>
<h2>全局 JavaScript 变量</h2>
<p>在函数外声明的变量是<em>全局</em>变量，网页上的所有脚本和函数都能访问它。</p>
<hr/>
<h2>JavaScript 变量的生存期</h2>
<p>JavaScript 变量的生命期从它们被声明的时间开始。</p>
<p>局部变量会在函数运行以后被删除。</p>
<p>全局变量会在页面关闭后被删除。</p>
<hr/>
<h2>向未声明的 JavaScript 变量分配值</h2>
<p>如果您把值赋给尚未声明的变量，该变量将被自动作为 window 的一个属性。</p>
<p>这条语句：</p>
<div class="code notranslate"><div>

carname="Volvo";</div></div>
<p>将声明 window 的一个属性 carname。</p>
<p>非严格模式下给未声明变量赋值创建的全局变量，是全局对象的可配置属性，可以删除。</p>
<pre>var var1 = 1; // 不可配置全局属性
var2 = 2; // 没有使用 var 声明，可配置全局属性

console.log(this.var1); // 1
console.log(window.var1); // 1
console.log(window.var2); // 2

delete var1; // false 无法删除
console.log(var1); //1

delete var2; 
console.log(delete var2); // true
console.log(var2); // 已经删除 报错变量未定义</pre> <div class="scroll-top"><a >返回顶部</a></div>
</div>

    </body>
    </html>
    